package com.gls.security.server.authorization.translator;

import com.gls.security.server.authorization.jpa.entity.Client;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;

/**
 * @author lizhiyong
 */
@Component
public class ClientTranslator {

    public ClientDetails translator(Client client) {
        if (client == null) {
            return null;
        }
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId(client.getClientId());
        clientDetails.setClientSecret(client.getClientSecret());
        clientDetails.setScope(client.getScope());
        clientDetails.setResourceIds(client.getResourceIds());
        clientDetails.setAuthorizedGrantTypes(client.getAuthorizedGrantTypes());
        clientDetails.setRegisteredRedirectUri(client.getRegisteredRedirectUris());
        clientDetails.setAutoApproveScopes(client.getAutoApproveScopes());
        clientDetails.setAccessTokenValiditySeconds(client.getAccessTokenValiditySeconds());
        clientDetails.setRefreshTokenValiditySeconds(client.getRefreshTokenValiditySeconds());
        clientDetails.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        return clientDetails;
    }
}
